<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ '编辑交易' if transaction else '添加交易' }} - 个人财务管理系统</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('index') }}">个人财务管理系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('index') }}">交易管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">财务报表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('budget') }}">预算管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('settings') }}">设置</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="messages mb-4">
                    {% for message in messages %}
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
        {% endwith %}

        <!-- 添加/编辑交易表单 -->
        <div class="card add-edit-card">
            <div class="card-header">
                <h2>{{ '编辑交易' if transaction else '添加交易' }}</h2>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ url_for('edit_transaction_route', transaction_id=transaction[0]) if transaction else url_for('add_transaction_route') }}">
                    <div class="mb-3">
                        <label for="type" class="form-label">交易类型</label>
                        <select id="type" name="type" class="form-select" required>
                            <option value="income" {% if transaction and transaction[1] == 'income' %}selected{% endif %}>收入</option>
                            <option value="expense" {% if transaction and transaction[1] == 'expense' %}selected{% endif %}>支出</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label for="amount" class="form-label">金额</label>
                        <div class="input-group">
                            <span class="input-group-text">¥</span>
                            <input type="number" id="amount" name="amount" class="form-control" step="0.01" min="0" placeholder="请输入金额" value="{{ '%.2f'|format(transaction[2]) if transaction else '' }}" required>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="category" class="form-label">类别</label>
                        <select id="category" name="category" class="form-select" required>
                            <option value="">请选择类别</option>
                            <optgroup label="收入类别" id="income-categories">
                                {% for category in income_categories %}
                                    <option value="{{ category }}" {% if transaction and transaction[3] == category and transaction[1] == 'income' %}selected{% endif %}>{{ category }}</option>
                                {% endfor %}
                            </optgroup>
                            <optgroup label="支出类别" id="expense-categories">
                                {% for category in expense_categories %}
                                    <option value="{{ category }}" {% if transaction and transaction[3] == category and transaction[1] == 'expense' %}selected{% endif %}>{{ category }}</option>
                                {% endfor %}
                            </optgroup>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label for="date" class="form-label">日期</label>
                        <input type="date" id="date" name="date" class="form-control" value="{{ transaction[4] if transaction else today }}" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="description" class="form-label">描述</label>
                        <textarea id="description" name="description" class="form-control" rows="3" placeholder="请输入描述">{{ transaction[5] if transaction and transaction[5] else '' }}</textarea>
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary btn-lg">
                            <i class="fa fa-save"></i> {{ '保存修改' if transaction else '添加交易' }}
                        </button>
                        <a href="{{ url_for('index') }}" class="btn btn-secondary btn-lg ml-2">
                            <i class="fa fa-arrow-left"></i> 返回
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>
    
    <script>
        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            // 根据交易类型显示相应的类别选项
            const typeSelect = document.getElementById('type');
            const incomeCategories = document.getElementById('income-categories');
            const expenseCategories = document.getElementById('expense-categories');
            
            function updateCategories() {
                if (typeSelect.value === 'income') {
                    incomeCategories.style.display = 'block';
                    expenseCategories.style.display = 'none';
                } else {
                    incomeCategories.style.display = 'none';
                    expenseCategories.style.display = 'block';
                }
            }
            
            // 初始化类别显示
            updateCategories();
            
            // 监听类型变化
            typeSelect.addEventListener('change', updateCategories);
        });
    </script>
</body>
</html>